/**
 * 
 */
package org.tuestudent.grouppa;

import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

import org.tuestudent.grouppa.AppContext.ActiveScreens;

import android.app.Activity;

/**
 * This class contains methods to run on the main ui thread, called from other
 * threads. It is used for example to show errors encountered in connection
 * thread.
 * 
 * @author Sylwester Kogowski
 * 
 */
public class UiController implements Runnable {

    private ConcurrentLinkedQueue<Task> tasks =
            new ConcurrentLinkedQueue<Task>();

    private static UiController instance = null;

    private UiController() {

    }

    public static UiController getInstance() {
        if (instance == null)
            instance = new UiController();
        return instance;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Runnable#run()
     */
    @Override
    public void run() {
        // TODO Auto-generated method stub
        if ( !tasks.isEmpty()) {
            AppContext ctx = Main.getContext();
            Task task = tasks.poll();
            switch (task.taskName) {
            case connectionError: {
                String connectionErrorText = (String) task.parameters;
                if (ctx.activeScreen == ActiveScreens.grouppaScreen) {
                    ctx.grouppaActivity
                            .connectionErrorPopup(connectionErrorText);
                } else if (ctx.activeScreen == ActiveScreens.login) {
                    ctx.loginActivity
                            .connectionErrorPopup("I am sorry, but we experienced problem with connecting to the server, we are currently retrying connection. Press cancel to stop");

                }
            }
                break;
            case applicationError: {
                String errorText = (String) task.parameters;
                if (ctx.activeScreen == ActiveScreens.grouppaScreen) {
                    ctx.grouppaActivity.errorPopup(errorText);
                } else if (ctx.activeScreen == ActiveScreens.login) {
                    ctx.loginActivity.errorPopup(errorText);
                }

            }
                break;
            case clearDialogs: {
                if (ctx.activeScreen == ActiveScreens.grouppaScreen) {
                    ctx.grouppaActivity.turnOffDialogs();
                } else if (ctx.activeScreen == ActiveScreens.login) {
                    ctx.loginActivity.turnOffDialogs();
                }
                
            }
                break;
            }
        }
    }

    enum taskID {
        connectionError, applicationError, clearDialogs
    }

    /**
     * add a task to the queue to be run on UI
     * 
     * @param taskName
     *            name of the task to run
     * @param parameters
     *            parameters for the task
     */
    public static void addTask(taskID taskName, Object parameters) {
        Task newTask = new Task();
        newTask.taskName = taskName;
        newTask.parameters = parameters;
        getInstance().tasks.add(newTask);
        Main.getContext().loginActivity.runOnUiThread(instance);
    }

    public static class Task {

        taskID taskName;
        Object parameters;
    }

}
